<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef">
      <el-row :gutter="20">
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
          <el-form-item label="姓名昵称" prop="agentId">
            <el-input
              v-model="queryParams.agentId"
              placeholder="请输入申请人姓名昵称"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
          <el-form-item label="申请时间" prop="createTime">
            <el-date-picker
              v-model="queryParams.createTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              :shortcuts="defaultShortcuts"
              :disabled-date="disabledDate"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
          <el-form-item>
            <el-button @click="handleQuery"
              ><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button
            >
            <el-button @click="resetQuery"
              ><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="ID" align="center" prop="agentId" min-width="80px" />
      <el-table-column label="代理名称" align="left" prop="agentName" min-width="100px">
        <template #default="scope">
          <div class="flex">
            <el-text>{{ scope.row.agentName }}</el-text>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="姓名|昵称" align="left" prop="avatar" min-width="220px">
        <template #default="scope">
          <div class="flex" v-if="scope.row.user">
            <div>
              <el-image
                :preview-teleported="true"
                :src="scope.row.user.avatar"
                class="w-[48px]! h-[48px]! m-r-10px rounded-4px align-middle"
              />
            </div>

            <el-text>{{ scope.row.user.nickname }}</el-text>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="手机" align="center" prop="agentPrice" min-width="100px" />

      <el-table-column label="上级代理商" align="left" prop="agentParentId" min-width="220px">
        <template #default="scope">
          <div class="flex" v-if="scope.row.agentParentId">
            <el-image
              :preview-teleported="true"
              :src="scope.row.parentUser.avatar"
              class="w-[48px]! h-[48px]! m-r-10px rounded-4px align-middle"
            />
            <div class="ml-10px text-left">
              <div> {{ scope.row.agentParentId }} </div>
              <div> {{ scope.row.parentUser.nickname }} </div>
            </div>
          </div>
          <div v-else> </div>
        </template>
      </el-table-column>

      <el-table-column label="邀请码" align="center" prop="brokeragePrice" min-width="180px">
        <template #default>
          <div class="flex flex-col">邀请码</div>
        </template>
      </el-table-column>
      <el-table-column
        label="申请时间"
        align="center"
        prop="agentParentTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="状态" align="center" prop="status" min-width="85">
        <template #default="scope">
          <el-tag v-if="scope.row.status == 0" type="primary">申请中</el-tag>
          <el-tag v-if="scope.row.status == 1" type="success">已通过</el-tag>
          <el-tag v-if="scope.row.status == 2" type="info">拒绝</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="150px" fixed="right">
        <template #default="scope">
          <div class="flex items-center justify-center">
            <el-button
              v-hasPermi="['trade:order:query']"
              link
              type="primary"
              @click="handleApply(scope.row.id)"
            >
              同意
            </el-button>
            <el-button
              v-hasPermi="['trade:order:query']"
              link
              type="primary"
              @click="handleApply(scope.row.id)"
            >
              拒绝
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import * as AgentUserApi from '@/api/mall/trade/agent/user'
import { defaultShortcuts } from '@/utils/formatTime'
const disabledDate = (time: Date) => {
  return time.getTime() > Date.now()
}
defineOptions({ name: 'TradeBrokerageUser' })

const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  bindUserId: null,
  agentId: undefined,
  agentLevelId: undefined,
  brokerageEnabled: true,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await AgentUserApi.getBrokerageUserPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleApply = async (row: AgentUserApi.AgentUserVO) => {
  try {
    let content = `是否通过代理${row.agentName}的申请`
    let params = {
      agentId: row.agentId,
      remark: ''
      // status: row.status == 0 ? 1 : 0
    }
    // 删除的二次确认
    await message.confirm(content)
    // 发起删除

    await AgentUserApi.updateStatus(params)
    message.success('操作成功')
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
onActivated(() => {
  getList()
})
</script>
